<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="auth" uri="http://www.recharge.com/authority" %>
<auth:authority role="0" />
<%--
这边要注意一个easyui 的问题，当panel tabs 这种href 引用页面
页面中不能有html body head 因为他只会把body 里面的内容 append 到页面中
--%>
<div id="dataCondition" class="easyui-panel"
     style="width:100% 1150px;height:150px;padding:10px;background:#fafafa;"
     data-options="closable:false,
                collapsible:false,minimizable:false,maximizable:false">
    <form id="saleprice-mainForm" method="post">

        <table>
            <tr>
                <td><label for="merchantId">供货商Id:</label></td>
                <td><input class="easyui-validatebox" type="text" name="merchantId" id="merchantId"/></td>
                <td><label for="salePrice">销售价格:</label></td>
                <td><input class="easyui-validatebox" type="text" name="salePrice" id="salePrice"/></td>
                <td><label for="productId">产品Id:</label></td>
                <td><input class="easyui-validatebox" type="text" name="productId" id="productId"/></td>
                <td><label for="productName">产品名称:</label></td>
                <td><input class="easyui-validatebox" type="text" name="productName" id="productName"/></td>
            </tr>

            <tr>
                <td colspan="3"></td>
                <td><input type="button" id="submit_saleprice-mainForm" value="查询" onclick="submitSaleMainForm();">
                </td>
            </tr>
        </table>
    </form>
</div>


<div id="dataView" class="easyui-panel" title="查询结果"
     style="width:100% 1150px;background:#fafafa; height: 415px;overflow-y:hidden"
     data-options="closable:false,
                collapsible:false,minimizable:false,maximizable:false">
    <table id="saleprice-dg">
    </table>
</div>

<div id="saleprice-detailDialog"></div>

<script language="JavaScript" type="text/javascript">

    $().ready(function () {

        //初始化查询结果表格
        $('#saleprice-dg').datagrid({
            columns:[[
                {field:'productId',title:'产品id',width:100},
                {field:'merchantId',title:'供货商id',width:100},
                {field:'productName',title:'产品名称',width:150},
                {field:'salePrice',title:'销售价格',align:'center',width:100,formatter: function(value,row,index){
                    return '<a href="#" onclick="updateSalepriceInfo(\''+row.productId+'\',\''+row.merchantId+'\',\''+row.productName+'\',\''+row.salePrice+'\');return false;"><font color="#7fff00">'+value+'</font></a>';
                }},
            ]],
            pagination: true,
            height: 390,
            toolbar: [{
                iconCls: 'icon-add',
                text : "添加新商品价格",
                handler: function(){
                    $('#saleprice-detailDialog').dialog({
                        title: '添加新商品价格',
                        width: 400,
                        height: 200,
                        closed: false,
                        cache: false,
                        href : "tradeSaleprice/salepriceAdd.jsp",
                        modal: true
                    });
                }
            },{
                iconCls: 'icon-add',
                text : "删除价格",
                handler: function(){
                $.messager.confirm('Confirm','确定删除?',function(r){
                	if (r){
                    	var selectOrder = $('#saleprice-dg').datagrid("getSelected");
                    	if(selectOrder==null){
                        	$.messager.alert('Warn','未选中要操作的订单');
                      	  	return;
                   		}
                    	var productId =selectOrder.productId;
                    	operTradeOrder(productId,"delete");
                    	$.messager.alert('Info','操作成功');
                	}
                });
                }
            }]
        });

        var pager = $('#saleprice-dg').datagrid().datagrid('getPager');
        pager.pagination({
            onBeforeRefresh:function(pageNumber, pageSize){
                getListData(pageNumber,pageSize);
            },
            onSelectPage:function(pageNumber, pageSize){
                getListData(pageNumber,pageSize);
            }
        });
    });
    
    function operTradeOrder(productId,type){
        $.post("saleprice/delete", {"productId":productId,"type":type},function(responseInfo){
            $('#saleprice-dg').datagrid('loadData', {
                "total":responseInfo.count,
                "rows":responseInfo.data
            });
        });
    }

    function getListData(pageNumber, pageSize){
        var param = $("#saleprice-mainForm").serializeArray();
        param.push({name:"curr",value:pageNumber});
        param.push({name:"pageSize",value:pageSize});
        
        $.post("saleprice/queryList", param,function(responseInfo){
            $('#saleprice-dg').datagrid('loadData', {
                "total":responseInfo.count,
                "rows":responseInfo.data
            });
        });
    }

    function submitSaleMainForm() {
        var param = $("#saleprice-mainForm").serializeArray();
        param.push({name:"curr",value:1});
        param.push({name:"pageSize",value:10});
        $.post("saleprice/queryList", param,function(responseInfo){
            $('#saleprice-dg').datagrid('loadData', {
                "total":responseInfo.count,
                "rows":responseInfo.data
            });
        });
    }
    
    function updateSalepriceInfo(productId,merchantId,productName,salePrice){

            if(salePrice!=null){
                $.messager.prompt('Confirm','输入你要修改后的值',function(r){
                    var param = [];
                    param.push({name:"productId",value:productId});
                    param.push({name:"merchantId",value:merchantId});
                    param.push({name:"productName",value:productName});
                    param.push({name:"salePrice",value:r});
                    $.post("saleprice/updateInfo", param,function(responseInfo){
                    });
                });
                return ;
            }
      }
</script>